import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/home',
    name: 'Home',
    redirect: '/welcome',
    component: () => import('../views/Home.vue'),
    children: [
      {
        path: '/welcome',
        name: 'Welcome',
        component: () => import('../components/home/homeWelcome.vue'),
      },
      {
        path: '/users',
        name: 'userusers',
        component: () => import('../components/user/userUsers.vue'),
      },
      {
        path: '/roles',
        name: 'rightsRole',
        component: () => import('../components/rights/rightsRole.vue'),
      },
      {
        path: '/rights',
        name: 'rightsList',
        component: () => import('../components/rights/rightsList.vue'),
      },
      {
        path: '/categories',
        name: 'goodsCate',
        component: () => import('../components/goods/goodsCate.vue'),
      },
      {
        path: '/params',
        name: 'goodsParams',
        component: () => import('../components/goods/goodsParams.vue'),
      },
      {
        path: '/goods',
        name: 'goodsList',
        component: () => import('../components/goods/goodsList.vue'),
      },
      {
        path: '/goods/add',
        name: 'goodsAdd',
        component: () => import('../components/goods/goodsAdd.vue'),
      },
      {
        path: '/goods/edit',
        name: 'goodsEdit',
        component: () => import('../components/goods/goodsEdit.vue'),
      },
      {
        path: '/orders',
        name: 'order',
        component: () => import('../components/orders/ordersList.vue'),
      },
      {
        path: '/reports',
        name: 'dataReport',
        component: () => import('../components/reports/dataReports.vue'),
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next) => {
  if(to.path === '/login') return next();
  const tokenStr = window.sessionStorage.getItem('token');
  if(tokenStr){
    return next()
  }else{
    return next('/login')
  }
})

export default router
